﻿var sqlHelper = require('./../common/SqlHelper');
var sql = require('mssql');

module.exports = {
    handlerName: 'ossManager',
    /*授权方法 */
    //查询授权列表
    queryAuthorizationList: function (query) {
        var sqlRequest = sqlHelper.request()
            .input('appId', sql.NVarChar, query.appId)
            .input('start', sql.Int, query.start)
            .input('limit', sql.Int, query.limit);

        // if (query.query != "" && query.query != null)
        //     sqlRequest.input('query', sql.NVarChar, "%" + query.query + "%");

        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pQueryAuthorizationList', (err, result) => {
            result = {
                items: result.recordset,
                total: result.returnValue
            };
            callBack(result, request);
        });
    },
    //保存授权应用
    saveAuthorization: function (query) {
        var sqlRequest = sqlHelper.request()
            .input('appId', sql.NVarChar, query.AppId)
            .input('key', sql.Int, query.Key)
            .input('tag', sql.Int, query.tag);//tag=0为添加

        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pSaveAuthorization', (err, result) => {
            if (err) {
                callBack(JSON.stringify(err), request);
            } else {
                callBack('ok', request);
            }
        });
    },
    //删除授权应用
    deleteAuthorization: function (id) {
        var sqlRequest = sqlHelper.request()
            .input('id', sql.Int, id);
        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pDeleteAuthorization', (err, result) => {
            if (err) {
                callBack(JSON.stringify(err), request);
            } else {
                callBack('ok', request);
            }
        });
    },

    /*消息方法 */
    //发送方应用下拉列表
    querySourceAppCombo: function (query) {
        var sqlRequest = sqlHelper.request();
        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pQuerySourceAppCombo', (err, result) => {
            if (err) {
                callBack(JSON.stringify(err), request);
            } else {
                result = { items: result.recordset };
                callBack(result, request);
            }
        });
    },
    //接收方应用下拉列表
    queryTargetAppCombo: function (query) {
        var sqlRequest = sqlHelper.request();
        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pQueryTargetAppCombo', (err, result) => {
            if (err) {
                callBack(JSON.stringify(err), request);
            } else {
                result = { items: result.recordset };
                callBack(result, request);
            }
        });
    },
    //消息主列表
    queryMessageMainList: function (query) {
        var sqlRequest = sqlHelper.request()
            .input('query', sql.NVarChar, query.query)
            .input('sourceAppId', sql.Int, query.sourceAppId)
            .input('startCreateTime', sql.NVarChar, query.startCreateTime)
            .input('endCreateTime', sql.NVarChar, query.endCreateTime)
            .input('pushed', sql.Int, query.pushed)
            .input('readed', sql.Int, query.readed)
            .input('deleted', sql.Int, query.deleted)
            .input('targetAppId', sql.Int, query.targetAppId)
            .input('startPushTime', sql.NVarChar, query.startPushTime)
            .input('endPushTime', sql.NVarChar, query.endPushTime)
            .input('messageType', sql.Int, query.messageType)
            .input('start', sql.Int, query.start)
            .input('limit', sql.Int, query.limit);

        if (query.query != "" && query.query != null)
            sqlRequest.input('query', sql.NVarChar, "%" + query.query + "%");

        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pQueryMessageMainList', (err, result) => {
            if (err) {
                callBack(JSON.stringify(err), request);
            } else {
                result = {
                    items: result.recordset,
                    total: result.returnValue
                };
                callBack(result, request);
            }
        });
    },

    /*用户登录 */
    //查询用户是否存在
    queryAccountExist: function (id) {
        var sqlRequest = sqlHelper.request()
            .input('id', sql.NVarChar, id);

        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pQueryAccountExist', (err, result) => {
            if (err) {
                callBack(JSON.stringify(err), request);
            } else {
                callBack(result.returnValue, request);
            }
        });
    },

    //用户登录
    loginByAccount: function (query) {
        var sqlRequest = sqlHelper.request()
            .input('account', sql.NVarChar, query.account)
            .input('password', sql.NVarChar, query.password);

        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pLoginByAccount', (err, result) => {
            if (err) {
                callBack(JSON.stringify(err), request);
            } else {
                result = {
                    record: result.recordset[0],
                    count: result.returnValue
                };
                callBack(result, request);
            }
        });
    },
    //保存账户
    saveAccount: function (query) {
        var sqlRequest = sqlHelper.request()
            .input('account', sql.NVarChar, query.account)
            .input('password', sql.NVarChar, query.password)
            .input('name', sql.NVarChar, query.name)
            .input('tag', sql.NVarChar, query.tag);

        var callBack = arguments[arguments.length - 2];
        var request = arguments[arguments.length - 1];
        sqlRequest.execute('dbo.pSaveAccount', (err, result) => {
            if (err) {
                callBack(JSON.stringify(err), request);
            } else {
                callBack('ok', request);
            }
        });
    }

}